package com.xintoucloud.pbs.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xintoucloud.pbs.entity.Organization;
import com.xintoucloud.pbs.entity.PartyMember;
import com.xintoucloud.pbs.entity.PartyMemberPermission;
import com.xintoucloud.pbs.entity.PartyMemberState;
import com.xintoucloud.pbs.model.DataGrid;
import com.xintoucloud.pbs.model.PartyDto;
import com.xintoucloud.pbs.service.PartyMemberService;
@Controller@RequestMapping("/partyMember")
public class PartyMemberController {
	
	@Autowired
	private PartyMemberService partyMemberService;
	
	/*@RequestMapping("/party")
	public String partyMember() {
		return "partyMember";
	}
	
	@GetMapping("/list")
	@ResponseBody
	public Map<String,Object> list() {
		List<PartyDto> list = partyMemberService.list();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("code", 0);
		map.put("data", list);
		return map;
	}*/
	
	@PostMapping("/delete")
	public void delete(HttpServletRequest request) {
		System.out.println("+++++++++++++++++++++++");
		String pid = request.getParameter("id");
		System.out.println("pid="+pid);
		int id = Integer.parseInt(pid);
		partyMemberService.deleteById(id);
	}
	
	@PostMapping("/add")
	public String add(HttpServletRequest request) {
		String pname = request.getParameter("pname");
		String ppswd = request.getParameter("ppswd");
		String rname = request.getParameter("rname");
		String sex = request.getParameter("sex");
		String idnum = request.getParameter("idnum");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		String state = request.getParameter("pms");
		String org = request.getParameter("org");
		String pmp = request.getParameter("pmp");
		PartyMember partyMember = new PartyMember();
		partyMember.setEmail(email);
		partyMember.setIdnum(idnum);
		partyMember.setPname(pname);
		partyMember.setPpswd(ppswd);
		partyMember.setRname(rname);
		partyMember.setSex(sex);
		partyMember.setPhone(phone);
		PartyMemberState states = new PartyMemberState ();
		states.setSid(Integer.parseInt(state));
		partyMember.setPms(states);
		PartyMemberPermission pmps = new PartyMemberPermission();
		pmps.setPpid(Integer.parseInt(pmp));
		partyMember.setPmp(pmps);
		Organization orgs = new Organization();
		orgs.setOid(Integer.parseInt(org));
		partyMember.setOrg(orgs);
		boolean result = partyMemberService.save(partyMember);
		if(result) {
			System.out.println("添加用户成功");
			return "redirect:party";
		}
		return "party";
	}
	
	@PostMapping("/update")
	public String update(HttpServletRequest request) {
		String pname = request.getParameter("name");
		String rname = request.getParameter("rname");
		String sex = request.getParameter("sex");
		String idnum = request.getParameter("idnum");
		String phone = request.getParameter("phone");
		String sid = request.getParameter("state");
		String id=request.getParameter("id");
		String email=request.getParameter("email");
		PartyMember partyMember = new PartyMember();
		partyMember.setPid(Integer.parseInt(id));
		partyMember.setIdnum(idnum);
		partyMember.setPname(pname);
		partyMember.setRname(rname);
		partyMember.setSex(sex);
		partyMember.setPhone(phone);
		partyMember.setEmail(email);
		PartyMemberState pms = new PartyMemberState();
		pms.setSid(Integer.parseInt(sid));
		partyMember.setPms(pms);
		partyMemberService.update(partyMember);
		return "redirect:party";
	}
	@RequestMapping("/party")
	public String partyMember_2() {
		return "partyMember";
	}
	@RequestMapping("/list")
	@ResponseBody
	 	public DataGrid<PartyDto> listPartyMemberByPage(HttpServletRequest request,HttpSession session) { 
		DataGrid<PartyDto> data=null;
		//1.获取参数
		 String page = request.getParameter("page");// 获取当前页 
		 String size = request.getParameter("limit");// 获取每页信息显示的条数 
		 //获取到当前登录的用户
		 PartyMember partyMember=(PartyMember) session.getAttribute("loginPartyMember");
		 int ppid=partyMember.getPmp().getPpid();
		 int oid = partyMember.getOrg().getOid();
		 //System.out.println("+++++++++++++++++++++++ppid=:"+ppid);
		 //System.out.println("+++++++++++++++++++++++oid=:"+oid);
		 int pages = Integer.parseInt(page); 
		 int rows = Integer.parseInt(size);  
		 if(ppid==1||ppid==2) {
		 // 查询总数
		 data=partyMemberService.list(pages,rows);
	 //党委
	 }else if(ppid == 3 ) {
		 System.out.println("党委 党委 党委 党委 党委党委党委党委党委党委");
		 data=partyMemberService.list2(pages,rows);
	  //党总支
	 }else if(ppid == 4) {
		 System.out.println("党总支 党总支 党总支 党总支 党总支 党总支 党总支 党总支 党总支");
		 data=partyMemberService.list3(pages, rows);
	 //党支部
	 }else if(ppid ==5) {
		 System.out.println("555555555555");
		 data=partyMemberService.list4(oid,pages,rows);
	 }
		 return data;
	}
}
